import {
  AvatarModel,
  CapchaModel,
  ChangePasswordRequest,
  LoginGetDto,
  LoginResponseDto,
} from './model';
import { ResponseStructure } from '@/model/responseModel';
import { RequestServiceProxy } from '@/utils/request';
import { request } from '@umijs/max';

const _request = new RequestServiceProxy();

// 获取图片验证码
export async function getImgCaptcha(): Promise<ResponseStructure<CapchaModel>> {
  return await _request.post<ResponseStructure<CapchaModel>>('/auth/capcha/query');
}

// 输入账号密码登录
export async function login(data: LoginGetDto): Promise<ResponseStructure<LoginResponseDto>> {
  return await _request.post<ResponseStructure<LoginResponseDto>>('/auth/user/login', data);
}

/** 获取当前的用户 GET /api/v0/system/auth/userinf */
export async function currentUser(): Promise<ResponseStructure<API.CurrentUser>> {
  return await _request.get<ResponseStructure<API.CurrentUser>>('/auth/user/userinfo');
}

/** 退出登录接口 POST /api/login/outLogin */
export async function outLogin() {
  return await _request.post<ResponseStructure<void>>('/auth/user/loginOut');
}

/** 修改密码 */
export async function changePassword(param: ChangePasswordRequest) {
  return await _request.post<ResponseStructure<void>>('/auth/user/changePassword', param);
}

/** 发送验证码 POST /api/login/captcha */
export async function getFakeCaptcha(
  params: {
    // query
    /** 手机号 */
    phone?: string;
  },
  options?: { [key: string]: any },
) {
  return request<API.FakeCaptcha>('/api/login/captcha', {
    method: 'GET',
    params: {
      ...params,
    },
    ...(options || {}),
  });
}

/** 上传头像 */
export async function uploadAvatar(param: AvatarModel) {
  return await _request.post<ResponseStructure<void>>('/auth/user/uploadAvatar', param);
}
